﻿using EMoney.IStock.Client.DataAccess.MC;
using EMoney.IStock.Client.Framework;
using EMoney.IStock.Client.Models.MC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EMoney.IStock.Client.Business.MC
{
    /// <summary>
    /// 在线时长业务逻辑类
    /// </summary>
    public class OnlineTimeBll
    {
        /// <summary>
        /// 完成在线时长任务
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public string CompleteOnlineTimeTask(int userid)
        {
            string ret = string.Empty;

            try
            {
                //int intervalMinutes = 30;
                DateTime dtNow = DateTime.Now;

                if (TradeDateBll.IsTradeDate(dtNow) && dtNow.TimeOfDay >= new TimeSpan(9, 25, 0) 
                    && dtNow.TimeOfDay <= new TimeSpan(15,5,0)) //交易日时段9点25分~15点5分
                {
                    MyInfoBll myInfoBll = new MyInfoBll();

                    MC_MyInfo myInfo = myInfoBll.GetMyInfo(userid);

                    if (myInfo != null)
                    {
                        int execFlag = 0;
                        OnlineTimeDal onlineTimeDal = new OnlineTimeDal();

                        MC_OnlineTime onlineTime = onlineTimeDal.GetOnlineTime(myInfo.UserID, dtNow.Date);

                        if (onlineTime != null)     //当天存在在线时长记录
                        {
                            TimeSpan timespan = dtNow - onlineTime.UpdateTime;

                            if (timespan.Minutes >= ConfigHelper.OnlineTimeIntervalMinutes) //距离上一次记录需大于30分钟
                            {
                                onlineTime.Minutes = onlineTime.Minutes + ConfigHelper.OnlineTimeMinutes;
                                onlineTime.UpdateTime = dtNow;

                                execFlag = onlineTimeDal.UpdateOnlineTime(onlineTime);
                            }
                        }
                        else
                        {
                            onlineTime = new MC_OnlineTime() { 
                                UserID = myInfo.UserID,
                                TradeDate = dtNow.Date,
                                Minutes = ConfigHelper.OnlineTimeMinutes,
                                UpdateTime = dtNow
                            };

                            execFlag = onlineTimeDal.AddOnlineTime(onlineTime);
                        }

                        if (execFlag == 1)  //新增或修改在线时长记录成功
                        {
                            int taskID = GetTaskID(myInfo, onlineTime);

                            if (taskID > 0)
                            {
                                TaskInfoBll taskInfoBll = new TaskInfoBll();

                                List<MC_TaskInfo> listTaskInfo = taskInfoBll.GetTaskInfoList();

                                if (listTaskInfo != null && listTaskInfo.Count > 0)
                                {
                                    MC_TaskInfo taskInfo = listTaskInfo.Find(list => list.TaskID == taskID);

                                    if (taskInfo != null)
                                    {
                                        if (taskInfoBll.CompleteTask(myInfo, taskID))   //完成任务 成功
                                        {
                                            MC_CompleteTaskRet objRet = new MC_CompleteTaskRet() { 
                                                tid = taskInfo.TaskID,
                                                inum = taskInfo.iNum,
                                                name = taskInfo.Name
                                            };

                                            ret = JsonHelper.ToJson(objRet);
                                        }
                                    }
                                }                                
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.DefaultLogger.Error(ex, string.Concat("完成在线时长任务失败.", userid));
            }

            return ret;
        }

        /// <summary>
        /// 获取可完成的在线时长任务编号
        /// </summary>
        /// <param name="myInfo"></param>
        /// <param name="listRecord"></param>
        /// <param name="onlineTime"></param>
        /// <returns></returns>
        private int GetTaskID(MC_MyInfo myInfo, MC_OnlineTime onlineTime)
        {
            int taskID = 0;

            if (myInfo.TodayCompleteTaskRecord != null && myInfo.TodayCompleteTaskRecord.Count > 0)
            {
                MC_CompleteTaskRecord record = myInfo.TodayCompleteTaskRecord.Find(lst => lst.TaskID == MC_TaskID.HalfHour.GetHashCode());  //在线半小时

                if (record == null && onlineTime.Minutes >= 30)
                {
                    taskID = MC_TaskID.HalfHour.GetHashCode();  //完成在线半小时任务
                }
                else
                {
                    record = myInfo.TodayCompleteTaskRecord.Find(lst => lst.TaskID == MC_TaskID.OneHour.GetHashCode()); //在线一小时

                    if (record == null && onlineTime.Minutes >= 60)
                    {
                        taskID = MC_TaskID.OneHour.GetHashCode();  //完成在线一小时任务
                    }
                    else
                    {
                        record = myInfo.TodayCompleteTaskRecord.Find(lst => lst.TaskID == MC_TaskID.TowHours.GetHashCode()); //在线两小时

                        if (record == null && onlineTime.Minutes >= 120)
                        {
                            taskID = MC_TaskID.TowHours.GetHashCode();  //完成在线两小时任务
                        }
                    }
                }
            }

            return taskID;
        }
    }
}
